import { getNewsList, getTabList } from "../../api/home"


// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    channels: [],
    refresher: false,
    isShow: false,
    query: {
      channel_id: 0,
      timestamp: Date.now(),
      with_top: 1
    },
    newList: []
  },

  goDetail(e) {
    const id = e.currentTarget.dataset.id
    
    wx.navigateTo({
      url: '/pages/detail/detail?id='+id
    })
  },

  onPulling() {
    this.setData({
      newList: [],
      'query.timestamp': Date.now()
    })
    this.getNewsList()
  },

  async getTabList() {
    const res = await getTabList()
    console.log(res);
    this.setData({
      channels: res.data.channels,
      isShow: true
    })
  },

  onTaps(name) {
    // console.log(name);
    const id = name.detail.name
    this.setData({
      newList: [],
      'query.channel_id': id,
      'query.timestamp': Date.now()
    })
    this.getNewsList()
  },

  async getNewsList() {
    const res = await getNewsList(this.data.query)
    console.log(res);
    this.setData({
      newList: [...this.data.newList,...res.data.results],
      refresher: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getTabList()
    this.getNewsList()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      'query.timestamp': Date.now()
    })
    this.getNewsList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})